package com.neusoft.elmboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.elmboot.po.vo.WalletTransactionVO;
import com.neusoft.elmboot.po.wallet.VirtualWallet;
import com.neusoft.elmboot.po.wallet.WalletTransaction;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface WalletMapper extends BaseMapper<VirtualWallet> {
    void setVIP(List<String> walletIdList);

    @Update("update elm.virtualwallet set overdraft_amount = #{amount} where id = #{walletId}")
    void setOverdraftAmount(String walletId, BigDecimal amount);

    @Update("update elm.virtualwallet set balance = balance + #{amount} where id = #{walletId}")
    void recharge(String walletId, BigDecimal amount);

    @Update("update elm.virtualwallet set balance = #{balance} where id = #{walletId}")
    void withdraw(String walletId, BigDecimal balance);

    @Insert("insert into elm.wallettransaction(trans_id, from_wallet_id, to_wallet_id, amount, is_frozen, is_valid) " +
            "values (#{transId}, #{fromWalletId}, #{toWalletId}, #{amount}, #{isFrozen}, #{isValid})")
    void addTrans(WalletTransaction transaction);

    @Select("select * from elm.wallettransaction where trans_id = #{transId}")
    WalletTransaction getTrans(String transId);

    void updateTransState(String transId, Boolean isFrozen, Boolean isValid);

    @Select("select trans_id from elm.wallettransaction where is_frozen = true")
    List<String> getFrozenTransId();

    @Select("select * from elm.virtualwallet where is_vip = true and current_overdraft_amount > 0")
    List<String> getOverdraftId();

    @Select("select from_wallet_id, to_wallet_id, amount, is_frozen, is_valid, create_time from elm.wallettransaction " +
            "where to_wallet_id = #{walletId}")
    List<WalletTransactionVO> getIncomeTrans(String walletId);

    @Select("select from_wallet_id, to_wallet_id, amount, is_frozen, is_valid, create_time from elm.wallettransaction " +
            "where from_wallet_id = #{walletId}")
    List<WalletTransactionVO> getOutcomeTrans(String walletId);

    @Select("select from_wallet_id, to_wallet_id, amount, is_frozen, is_valid, create_time from elm.wallettransaction " +
            "where to_wallet_id = #{walletId} or from_wallet_id = #{walletId}")
    List<WalletTransactionVO> getTransByWalletId(String walletId);
}
